package com.wuxu.huanfu

import android.content.Context
import android.content.pm.PackageManager
import android.content.res.AssetManager
import android.content.res.Resources

/**
 *@author wuxu
 *@date 2024/5/16 18:43
 *@description 资源管理
 *
 * 换肤核心功能：
 * 1. resource 替换为皮肤包
 * 2. Activity加载resource的内容
 *
 * Factory替换
 *
 *
 */
class ResourceMgr {
    var mResource:Resources? = null
    var mSkinPackageName:String? = null

    fun loadSkinAppResource(context:Context,skinApkPath:String){
        val assetManager:AssetManager
        try {
           val packageInfo = context.packageManager.getPackageArchiveInfo(skinApkPath,PackageManager.GET_ACTIVITIES)
            mSkinPackageName = packageInfo?.packageName
//            mSkinPackageName = context.packageName

            //创建classLoader
            assetManager = AssetManager::class.java.newInstance()
            val addAssetPathMethod = assetManager.javaClass.getDeclaredMethod("addAssetPath",String::class.java)
            addAssetPathMethod.isAccessible = true
            addAssetPathMethod.invoke(assetManager,skinApkPath)

            mResource = Resources(assetManager,context.resources.displayMetrics,context.resources.configuration)
        }catch(e:Exception){
            e.printStackTrace()
        }
    }
}